﻿using System.ComponentModel;
using System.Web.UI;

namespace Altairis.Web.UI.WebControls {

    /// <summary>
    /// Control for displaying link with shortened text. If text is longer than <c>MaximumTextLength</c> characters, is shortened and configured <c>EllipsisText</c> is added.
    /// </summary>
    public class EllipsisHyperLink : System.Web.UI.WebControls.HyperLink {

        public EllipsisHyperLink() {
            this.MaximumTextLength = 0;
            this.EllipsisText = "&hellip;";
            this.TrimMode = EllipsisTrimMode.TrimEnd;
        }

        /// <summary>
        /// Gets or sets text used as ellipsis character. By default, HTML entity for ellipsis (<c>&hellip;</c>) is used.
        /// </summary>
        [Localizable(true), DefaultValue("&hellip;"), Category("Appearance")]
        [Description("Gets or sets text used as ellipsis character. By default, HTML entity for ellipsis is used.")]
        public string EllipsisText {
            get { return (string)this.ViewState["EllipsisText"]; }
            set { this.ViewState["EllipsisText"] = value; }
        }

        /// <summary>
        /// Gets or sets maximum length of text. If zero, length is unlimited.
        /// </summary>
        [DefaultValue(0), Category("Appearance")]
        [Description("Gets or sets maximum length of text. If zero, length is unlimited.")]
        public int MaximumTextLength {
            get { return (int)this.ViewState["MaximumTextLength"]; }
            set { this.ViewState["MaximumTextLength"] = value; }
        }

        /// <summary>
        /// Gets or sets if long text should be trimmed on beginning or on end.
        /// </summary>
        [DefaultValue(EllipsisTrimMode.TrimEnd), Category("Appearance")]
        [Description("Gets or sets if long text should be trimmed on beginning or on end.")]
        public EllipsisTrimMode TrimMode {
            get { return (EllipsisTrimMode)this.ViewState["TrimMode"]; }
            set { this.ViewState["TrimMode"] = value; }
        }

        protected override void Render(HtmlTextWriter writer) {
            if (string.IsNullOrEmpty(this.Text) || this.MaximumTextLength == 0 || this.Text.Length <= this.MaximumTextLength) {
                // Special cases, nothing to do, call normal label render
                base.Render(writer);
            }
            else {
                // We have work here; set tooltip to full text if not set otherwise
                if (string.IsNullOrEmpty(this.ToolTip)) this.ToolTip = this.Text;

                // Shorten text and add ellipsis
                string s = this.Text;
                if (this.TrimMode == EllipsisTrimMode.TrimEnd) {
                    this.Text = this.Text.Substring(0, this.MaximumTextLength) + this.EllipsisText;
                }
                else {
                    this.Text = this.EllipsisText + this.Text.Substring(this.Text.Length - this.MaximumTextLength);
                }
                base.Render(writer);
                this.Text = s;
            }
        }
    }
}